/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */
package org.apache.cordova;

import javafx.application.Platform;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * This class exposes methods in DroidGap that can be called from JavaScript.
 * FIXME CJO - not finish at all
 * 
 * @author Cyril Joui
 */
public class App extends CordovaPlugin {

	public static final String TAG = "App";

	/**
	 * Constructor.
	 */
	public App() {
	}

	/**
	 * Executes the request and returns PluginResult.
	 * 
	 * @param action
	 *            The action to execute.
	 * @param args
	 *            JSONArry of arguments for the plugin.
	 * @param callbackContext
	 *            The callback id used when calling back into JavaScript.
	 * @return True if the action was valid, false if not.
	 */
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		if (action.equals("clearCache")) {
			this.clearCache();
		} else if (action.equals("show")) {
			/*
			 * // This gets called from JavaScript onCordovaReady to show the
			 * webview. // I recommend we change the name of the Message as
			 * spinner/stop is not // indicative of what this actually does
			 * (shows the webview). cordova.getActivity().runOnUiThread(new
			 * Runnable() { public void run() { webView.postMessage("spinner",
			 * "stop"); } });
			 */
		} else if (action.equals("loadUrl")) {
			// this.loadUrl(args.getString(0), args.optJSONObject(1));
		} else if (action.equals("cancelLoadUrl")) {
			// this.cancelLoadUrl();
		} else if (action.equals("clearHistory")) {
			this.clearHistory();
		} else if (action.equals("backHistory")) {
			this.backHistory();
		} else if (action.equals("overrideButton")) {
			// this.overrideButton(args.getString(0), args.getBoolean(1));
		} else if (action.equals("overrideBackbutton")) {
			// this.overrideBackbutton(args.getBoolean(0));
		} else if (action.equals("exitApp")) {
			this.exitApp();
		} else {
			return false;
		}
		return true;
	}

	/**
	 * Exit application.
	 */
	private void exitApp() {
		Platform.exit();
	}

	private void backHistory() {
		// TODO Auto-generated method stub

	}

	private void clearHistory() {
		// TODO Auto-generated method stub

	}

	private void clearCache() {
		// TODO Auto-generated method stub

	}
}
